"""
TPP for galaxy
"""
import binary, data
import logging
import binascii
from galaxy.datatypes.metadata import MetadataElement
from galaxy.datatypes import metadata
from galaxy.datatypes.sniff import *

#from urllib import urlencode, quote_plus
import zipfile, gzip
#import os, subprocess, tempfile

log = logging.getLogger(__name__)


class Splib( binary.Binary ):
	""" Class describing a spectral library (splib)"""
	file_ext = "splib"	

class SpectraST( data.Text ):
	""" Generic data type"""
	file_ext = "spectrast"
	def get_mime(self):
		return 'text/plain'
		
class Sptxt( SpectraST ):
	file_ext = "sptxt"
	
class Spidx( SpectraST ):
	file_ext = "spidx"
	
class Pepidx( SpectraST ):
	file_ext = "pepidx"

class Pepxml( data.Text ):
	""" XML-based Search Output"""
	file_ext = "pep.xml"
	def get_mime(self):
		"""
		Return the mime type
		"""
		return 'text/xml'
class MzXML( data.Text ):
	file_ext = "mzXML"
	def get_mime(self):
		return 'text/xml'

class Tppfileset( SpectraST ):
    """Class describing a zip archive of files"""
    file_ext = "tppfileset"
    def sniff(self, filename):
	try:
		if zipfile.is_zipfile(filename):
                        return True
                else:
                        return False
	except:
            return False
    def set_peek( self, dataset, is_multi_byte=False ):
        if not dataset.dataset.purged:
            zip_file = zipfile.ZipFile( dataset.file_name, "r" )
            num_files = len( zip_file.namelist() )
            dataset.peek  = "Archive of %s files" % ( str( num_files ) )
            dataset.blurb = data.nice_size( dataset.get_size() )
        else:
            dataset.peek = 'file does not exist'
            dataset.blurb = 'file purged from disk'
    def display_peek(self, dataset):
        try:
            return dataset.peek
        except:
            return "Zip file archive (%s)" % ( data.nice_size( dataset.get_size() ) )
    def get_mime(self):
        """Returns the mime type of the datatype"""
        return 'application/zip'
